#include<bits/stdc++.h>
using namespace std;

#define int long long 
#define mod 998244353
#define inf 0x3f3f3f3f
#define N 200010
int n,m,k,t;
int an[N],a[N],ca[N],c[N],pre[N],b[N],x[4],as,id[N];
int d[30][N],v[N],bo;
int solve(int x){
    int ans=0,cnt=1;pre[ans]=cnt;
    while(cnt<x){
        cnt<<=1;ans++;
        pre[ans]=cnt;
    }
    return ans;
}
void count(int n){
    for(int i=1;i<=n;++i)id[i]=i;
    int k=solve(n);
    for(int i=1;i<=k;++i){
        for(int j=1;j<=pre[k-i];++j){
            int x=id[j*2-1],y=id[j*2];
            if(d[i][j]==1)swap(x,y);
            if(ca[x]>=i)id[j]=x;
            else id[j]=y;
        }
    }
    v[id[1]]=1;
    return;
}
void dfs(int x,int y,int l){
    if(x>y){count(y);return;}
    for(int i=0;i<=l;++i){
        ca[x]=i;
        dfs(x+1,y,l);
    }
    return;
}
int solve3(int n){
    as=0;
    for(int i=1;i<=n;++i)ca[i]=a[i];
    int k=solve(n);
    dfs(n+1,pre[k],k+1);
    for(int i=1;i<=pre[k];++i){
        if(v[i])as+=i;
        v[i]=0;
    }
    return as;
}
void solve2(){
    int ans=0;
    for(int i=1;i<=m;++i){
        ans^=(i*solve3(c[i]));
    }
    printf("%lld\n",ans);
    return;
}
signed main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    scanf("%lld%lld",&n,&m);
    for(int i=1;i<=n;++i)scanf("%lld",&an[i]);
    for(int i=1;i<=m;++i){scanf("%lld",&c[i]);if(pre[solve(c[i])]!=c[i])bo=1;}
    k=solve(n);
    for(int i=1;i<=k;++i){
        for(int j=1;j<=pre[k-i];++j){
            char c;cin>>c;
            d[i][j]=c-'0';
        }
    }
    scanf("%lld",&t);
    while(t--){
        for(int i=0;i<=3;++i)scanf("%lld",&x[i]);
        for(int i=1;i<=n;++i)a[i]=an[i]^x[i%4];
        if(n<=8||bo==0){solve2();continue;}
    }
    return 0;
}